package com.cg.managesystem.controller;

import com.cg.managesystem.common.JSONResult;
import com.cg.managesystem.entity.Order;
import com.cg.managesystem.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created on 2019/8/13 by Tinchi
 **/
@RestController

public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 查询所有订单
     */
    @PostMapping(value = "/findAllOrders")
    public List<Order> findAllOrders() {
        return orderService.findAllOrders();
    }

    /**
     * 按Id查找订单
     */
    @PostMapping(value = "/findOrderById")
    public Order findOrderById(Integer id) {
        return orderService.findOrderById(id);
    }

    /**
     * 新增订单
     */
    @PostMapping(value = "/user/CreateOrder")
    public JSONResult addOrder(@RequestParam Integer uid,
                               @RequestParam Integer rid,
                               @RequestParam Integer status
    ) {
        Order order = new Order();
        order.setUid(uid);
        order.setRid(rid);
        order.setOrderNumber(UUID.randomUUID().toString().replace("-", "").substring(0, 9));
        order.setDatetime(new Date());
        order.setStatus(status);

        int i = orderService.addOrder(order);
        if (i != 0) {
            return JSONResult.ok("添加订单成功");
        } else {
            return JSONResult.errorMsg("添加订单失败");

        }

    }

    /**
     * 按ID更新订单
     */
    @PostMapping(value = "/updateOrderById")
    public int updateOrderById(Order order) {
        return orderService.updateOrderById(order);
    }

    /**
     * 按ID删除订单
     */
    @PostMapping(value = "/deleteOrderById")
    public int deleteOrderById(Integer id) {
        return orderService.deleteOrderById(id);
    }

    /**
     *  按orderId更新status
     * @param order
     * @return
     */
    @PostMapping( value = "/updateOrderStatus" )
    public JSONResult updateOrderStatus(Order order) {
        int i = orderService.updateByPrimaryKey(order);
        System.out.println(i);
        return i == 1? JSONResult.ok("更新成功"):JSONResult.errorMsg("更新失败");
    }

}
